package org.ht.viewer;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

import org.azj.inireader.FieldBasicInfo;
import org.azj.logHelper.LOG;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.ht.config.ColumnInfoXmlReader;

public class MainEntrance {

	/**
	 * 主程序入口
	 * 
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) {
		try {
			LOG.WriteLog("系统启动");
			new MainFrame();
			// TODO
			// readConfig();
			// TODO 自动化设置XML先注释掉
			// setXML();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// static DBFieldIniReader iniReaderHelper;
	// public static void readConfig() {
	// iniReaderHelper = new DBFieldIniReader();
	//
	// int readLines = 0;
	// try {
	// readLines = iniReaderHelper.read("config\\LH_DB_Field.ini");
	// System.out.println(String.format("%d lines have been read",
	// readLines));
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	public static void setXML() throws Exception {
		Document document = new SAXReader().read("xml\\CaseDialog.xml");
		Element root = document.getRootElement();
		doit(root);
		XMLWriter writer = new XMLWriter(new FileOutputStream(
				"xml\\CaseDialog.xml"));
		writer.write(document);
		writer.close();
	}

	private static void doit(Element e) {
		List<Element> els = e.elements();
		HashMap<String, FieldBasicInfo> map = ColumnInfoXmlReader
				.getXmlReader().getValuesByFields();
		for (Element el : els) {
			Attribute aname = el.attribute("name");
			if (aname != null) {
				String name = aname.getValue();
				FieldBasicInfo fbi = map.get(name);
				if (fbi != null) {
					el.addAttribute("id", fbi.FieldName);
				}
			}

			if (el.attribute("name") != null) {

			}
			// 判断是否复合节点
			if (el.hasMixedContent()) {
				doit(el);
			}
		}
	}
}
